package com.aispeech.asr.comm.data.vo;

import com.aispeech.asr.comm.enums.APICode;
import com.aispeech.asr.comm.enums.ErrorCode;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.AllArgsConstructor;
import lombok.Data;

@SuppressWarnings("unchecked")
@Data
@AllArgsConstructor(staticName = "of")
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class ResultOnline<T> {
    private static final long serialVersionUID = 1L;

    private int errno;
    private int eof;
    private String error;
    private T data;

    public static ResultOnline ok() {
        return ok(APICode.OK.getCode(), APICode.OK.getMessage(), null);
    }

    public static <T> ResultOnline ok(T result) {
        return ok(APICode.OK.getCode(), APICode.OK.getMessage(), result);
    }
    
    public static <T> ResultOnline ok(T result,int eof) {
        return ok(APICode.OK.getCode(), eof ,APICode.OK.getMessage(), result);
    }

    private static <T> ResultOnline result(int code, String message, T data) {
        return new ResultOnline(code, 0, message, data);
    }
    
    private static <T> ResultOnline result(int code, int eof ,String message, T data) {
        return new ResultOnline(code, eof, message, data);
    }

    public static <T> ResultOnline ok(int code, String message, T data) {
        return result(code, message, data);
    }
    
    public static <T> ResultOnline ok(int code, int eof, String message, T data) {
        return result(code, eof, message, data);
    }

    public static ResultOnline fail() {
        return fail(APICode.INTERNAL_ERROR);
    }

    public static ResultOnline fail(APICode err) {
        return fail(err.getCode(), err.getMessage());
    }

    public static ResultOnline fail(ErrorCode err) {
        return fail(err.errid, err.error);
    }

    public static ResultOnline fail(int code, String message) {
        return fail(code, message, null);
    }

    public static <T> ResultOnline fail(int code, String message, T data) {
        return result(code, 1, message, data);
    }
}
